﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    public class Menu
    {
        /* Textures */
        // Base
        ParticleManager particleManager;
        Texture2D background, title, doodad1, doodad2;
        Vector2 titlePos;
       
        //Main
        Texture2D mainPlay, mainOptions, mainQuit;
        Texture2D mainPlayOK, mainOptionsOK, mainQuitOK;
        Vector2 mainPlayPos, mainOptionsPos, mainQuitPos;

        // Variables
        int selected = 0;
        KeyboardState kState, kPreviousState;

        /* Other */
        Song scroll;

        public enum Screen
        {
            Main,
            Play,
            Option,
            Option1,
            Score,
            Pause,
            None
        }
        Screen currentScreen = Screen.Main;

        PlatformerGame game;

        public Menu(PlatformerGame game)
        {
            this.game = game;
        }

        public void LoadContent()
        {
            LoadBase();
            LoadMain();
        }

        public void Update()
        {
            kPreviousState = kState;
            kState = Keyboard.GetState();

            UpdateBase();
            if (currentScreen == Screen.Main)
                UpdateMain();
            else if (currentScreen == Screen.Pause)
                UpdatePause();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (currentScreen != Screen.None && currentScreen != Screen.Pause)
            {
                DrawBase(spriteBatch);
                DrawDecorations(spriteBatch);
                if (currentScreen == Screen.Main)
                    DrawMain(spriteBatch);
            }
            else if (currentScreen == Screen.Pause)
            {

            }
        }

        /* Custom */
        // Base of the menu
        void LoadBase()
        {
            background = game.Content.Load<Texture2D>("Menu/Base/background");
            title = game.Content.Load<Texture2D>("Menu/Base/title");
            scroll = game.Content.Load<Song>("Audio/menu_scroll");
            doodad1 = game.Content.Load<Texture2D>("Menu/Base/doodad1");
            doodad2 = game.Content.Load<Texture2D>("Menu/Base/doodad2");
            particleManager = new ParticleManager(game.Content);

            titlePos = new Vector2(230, 0);
        }

        void UpdateBase()
        {
            particleManager.Update();
        }

        void DrawBase(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            particleManager.Draw(spriteBatch);
            spriteBatch.Draw(title, titlePos, Color.White);
            spriteBatch.Draw(doodad2, new Vector2(100, 50), Color.White);
            spriteBatch.Draw(doodad1, new Vector2(860, 50), Color.White);
        }

        void DrawDecorations(SpriteBatch spriteBatch)
        {
        }

        // Main menu elements
        void LoadMain()
        {
            mainPlay = game.Content.Load<Texture2D>("Menu/Main/play");
            mainOptions = game.Content.Load<Texture2D>("Menu/Main/options");
            mainQuit = game.Content.Load<Texture2D>("Menu/Main/quit");

            mainPlayOK = game.Content.Load<Texture2D>("Menu/Main/playOK");
            mainOptionsOK = game.Content.Load<Texture2D>("Menu/Main/optionsOK");
            mainQuitOK = game.Content.Load<Texture2D>("Menu/Main/quitOK");

            mainPlayPos = new Vector2(PlatformerGame.WIDTH / 2 - mainPlay.Width / 2, 350);
            mainOptionsPos = new Vector2(PlatformerGame.WIDTH / 2 - mainOptions.Width / 2, 430);
            mainQuitPos = new Vector2(PlatformerGame.WIDTH / 2 - mainQuit.Width / 2, 510);
        }

        void UpdateMain()
        {
            // Controls
            if (kState.IsKeyDown(Keys.Down) && !kPreviousState.IsKeyDown(Keys.Down))
            {
                if (selected == 2)
                    selected = 0;
                else
                    selected++;
                MediaPlayer.Play(scroll);
            }
            else if (kState.IsKeyDown(Keys.Up) && !kPreviousState.IsKeyDown(Keys.Up))
            {
                if (selected == 0)
                    selected = 2;
                else
                    selected--;
                MediaPlayer.Play(scroll);
            }

            // Events
            if (kState.IsKeyDown(Keys.Enter))
            {
                // Play
                if (selected == 0)
                {
                    currentScreen = Screen.None;
                    game.gameState = PlatformerGame.GameState.Ingame;
                }
                else if (selected == 1)
                    currentScreen = Screen.Option;
                else if (selected == 2)
                    game.Exit();
            }
        }

        void DrawMain(SpriteBatch spriteBtach)
        {
            if(selected == 0)
                spriteBtach.Draw(mainPlayOK, mainPlayPos, Color.White);
            else
                spriteBtach.Draw(mainPlay, mainPlayPos, Color.White);
            if(selected == 1)
                spriteBtach.Draw(mainOptionsOK, mainOptionsPos, Color.White);
            else
                spriteBtach.Draw(mainOptions, mainOptionsPos, Color.White);
            if (selected == 2)
                spriteBtach.Draw(mainQuitOK, mainQuitPos, Color.White);
            else
                spriteBtach.Draw(mainQuit, mainQuitPos, Color.White);
        }


        // Pause menu
        void LoadPause()
        {

        }

        void UpdatePause()
        {

        }

        void DrawPause()
        {

        }

        /* Other functions */

    }
}
